﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text.RegularExpressions;

/// <summary>
/// Summary description for FCKLoadedManager
/// </summary>
public class FCKLoadedManager
{
    public string userid;
    private Regex Rer;
    public string uploadurl;
   
    public FCKLoadedManager(string userid)
    {
        if (userid == null) return;
        this.userid = userid.ToString();
        this.uploadurl = HttpContext.Current.Server.MapPath("/") + "uploadfiles/" + userid + "/";
        string p = "src=\"/uploadfiles/" + userid + "/";
        Rer = new Regex(p + @"[^>]+?((\.jpg)|(\.gif)|(\.jpeg)|(\.png)|(\.bmp))", RegexOptions.Compiled | RegexOptions.IgnoreCase);
    }
    
    public string getFileListFromInfo(string info)
    {
        MatchCollection mymc = Rer.Matches(info);
        string str = "";
        for (int i = 0; i < mymc.Count; i++)
        {
            str += mymc[i].Value + "|";
        }
        return str;
    }
    public void deleteImages()
    {
        if (System.IO.File.Exists(uploadurl + "temp.txt"))
        {
            try
            {
                System.IO.FileStream fs = new System.IO.FileStream(uploadurl + "temp.txt", System.IO.FileMode.Open);
                System.IO.StreamReader sr = new System.IO.StreamReader(fs);
                string img = sr.ReadLine();
                while (img != null)
                {
                    if (System.IO.File.Exists(uploadurl + img))
                    {
                        System.IO.File.Delete(uploadurl + img);
                    }
                    img = sr.ReadLine();
                }
                sr.Close();
            }
            catch (Exception e) { }
            System.IO.File.Delete(uploadurl + "temp.txt");
        }
    }
    public void deleteImages(string info)
    {
        string list=this.getFileListFromInfo(info);
        if(list!="")
        {
            string[] lists=list.Split('|');
            string name;
            try
            {
                for (int i = lists.Length - 1; i >= 0; i--)
                {
                    name = lists[i].Substring(lists[i].LastIndexOf("/") + 1);
                    if (System.IO.File.Exists(uploadurl + name))
                    {
                        System.IO.File.Delete(uploadurl + name);
                    }
                }
            }
            catch (Exception e) { }
        }
    }
    public void deleteImgBeforeInsert(string info)
    {
        string filelist = getFileListFromInfo(info);
        string[] imgs = { "" };
        if (HttpContext.Current.Session["imgs"] != null) imgs = HttpContext.Current.Session["imgs"].ToString().Split('|');
        else return;
        for (int i = imgs.Length - 1; i >= 0; i--)//删除服务器多余图片
        {
            if (filelist.IndexOf(imgs[i]) == -1)
            {
                if (System.IO.File.Exists(uploadurl + imgs[i]))
                    System.IO.File.Delete(uploadurl + imgs[i]);
            }
        }
        System.IO.File.Delete(uploadurl + "temp.txt");
        HttpContext.Current.Session.Remove("imgs");
    }
    public void deleteUserForder()
    {
        try
        {
            if (System.IO.Directory.Exists(uploadurl))
            {
                // 循环文件夹里面的内容   
                foreach (string f in System.IO.Directory.GetFileSystemEntries(this.uploadurl))
                {
                    // 如果是文件存在        
                    if (System.IO.File.Exists(f))
                    {
                        // 直接删除其中的文件          
                        System.IO.File.Delete(f);
                    }
                }
                // 删除已空文件夹   
                System.IO.Directory.Delete(uploadurl);
            }
        }
        catch (Exception ex) { }
    }
}
